// Quantitative Angiogram Analysis System
//
// Copyright ?Ping Zou, 2006
// sg71.cherub@gmail.com

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

using WeifenLuo.WinFormsUI;
using eee.Sheffield.PZ.Imaging;
using eee.Sheffield.PZ.Imaging.ImageIO;

namespace eee.Sheffield.PZ.QAAS
{
    /// <summary>
    /// class MainForm menu item methods
    /// </summary>
    partial class MainForm
    {
        /// <summary>
        /// open image file
        /// </summary>
        private void OpenImage()
        {
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                ImageDoc imageDoc = null;

                try
                {
                    // create image document
                    imageDoc = new ImageDoc(ofd.FileName);
                    imageDoc.ImageFileName = ofd.FileName;
                    // change current directory
                    Directory.SetCurrentDirectory(Path.GetDirectoryName(imageDoc.ImageFileName));
                    imageDoc.TabText = Path.GetFileName(ofd.FileName);
                }
                catch (ApplicationException e)
                {
                    String errorText = "QAAS::OpenFile(), " + e.Message;
                    MessageBox.Show(errorText, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    // log
                    Log(errorText);
                }
                if (imageDoc != null)
                {
                    string logText = "Image <" + imageDoc.TabText + "> is opened.";
                    Log(logText);
                    imageDoc.Show(_dockPanelMainForm);
                    imageDoc.Focus();
                }
            }
        }

        /// <summary>
        /// save image file
        /// </summary>
        private void SaveImage()
        {
            ImageDoc imageDoc = _dockPanelMainForm.ActiveContent as ImageDoc;
            try
            {
                if (imageDoc != null)
                {
                    // set initial file name
                    if (imageDoc.ImageFileName != null)
                        sfd.FileName = Path.GetFileName(imageDoc.ImageFileName);
                    else
                        sfd.FileName = imageDoc.Text + ".bmp";

                    sfd.FilterIndex = 0;

                    // show dialog
                    if (sfd.ShowDialog(this) == DialogResult.OK)
                    {
                        string fileName = sfd.FileName;
                        Bitmap image = imageDoc.Image;
                        IImageIO imageIO = null;

                        // check file name extension to choose corresponding ImageIO class
                        string extension = Path.GetExtension(fileName);
                        extension = extension.ToLower();
                        bool isSupported = true;
                        switch (extension)
                        {
                            case ".pgm":
                                imageIO = new PGMImageIO(fileName, image);
                                break;
                            case ".ppm":
                                imageIO = new PPMImageIO(fileName, image);
                                break;
                            case ".dcm":
                                imageIO = new DICOMImageIO(fileName, image);
                                break;
                            case ".bmp":
                                imageIO = new BMPImageIO(fileName, image);
                                break;
                            case ".jpg":
                                imageIO = new JPGImageIO(fileName, image);
                                break;
                            case ".png":
                                imageIO = new PNGImageIO(fileName, image);
                                break;
                            case ".tif":
                                imageIO = new TIFImageIO(fileName, image);
                                break;
                            default:
                                isSupported = false;
                                break;
                        }

                        if (!isSupported)
                            throw new ApplicationException("ImageIO::SaveImage(), Unkown image type");
                        else
                        {
                            imageIO.SaveImage();
                            // change current directory
                            Directory.SetCurrentDirectory(Path.GetDirectoryName(fileName));
                            // log
                            string logText = "Image <" + sfd.FileName + "> is saved";
                            Log(logText);
                        }
                        imageDoc.TabText = Path.GetFileName(sfd.FileName);
                        imageDoc.ImageFileName = sfd.FileName;
                    }                   
                }                
            }
            catch (ApplicationException e)
            {
                String errorText = "QAAS::SaveFile(), " + e.Message;
                MessageBox.Show(errorText, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                // log
                Log(errorText);
            }
        }

        /// <summary>
        /// show histogram form
        /// </summary>
        public void ShowHistogramForm()
        {
            if (histogramForm == null || histogramForm.IsDisposed)
            {
                histogramForm = new HistogramForm();
            }
            if (!histogramForm.IsActivated)
            {
                histogramForm.Show(_dockPanelMainForm, DockState.DockRight);
                histogramForm.TabText = "Histogram";
                histogramForm.DockState = configuration.showHistogramForm ? DockState.DockRight : DockState.DockRightAutoHide;
            }
        }

        /// <summary>
        /// show filter form
        /// </summary>
        public void ShowFilterForm()
        {
            if (filterForm == null || filterForm.IsDisposed)
            {
                filterForm = new FilterForm();
            }
            //filterForm.MainForm = this;
            if (!filterForm.IsActivated)
            {
                filterForm.Show(_dockPanelMainForm, DockState.DockRight);
                filterForm.TabText = "Filter Settings";
                filterForm.DockState = configuration.showFilterForm ? DockState.DockRight : DockState.DockRightAutoHide;
            }
        }

        /// <summary>
        /// show log form
        /// </summary>
        public void ShowLogForm()
        {
            if (logForm == null || logForm.IsDisposed)
            {
                logForm = new LogForm();
            }
            if (!logForm.IsActivated)
            {
                logForm.Show(_dockPanelMainForm, DockState.DockBottomAutoHide);
                string logText = "QAAS sucessfully started at: " + DateTime.Now.Date.ToString();
                logForm.txtLog.Text = logText;
                logForm.DockState = configuration.showLogForm ? DockState.DockBottom : DockState.DockBottomAutoHide;
            }
        }

        /// <summary>
        /// update log
        /// </summary>
        /// <param name="logText"></param>
        public void Log(string newLogText)
        {
            string logText = logForm.txtLog.Text;
            logText = logText + "\r\n" + newLogText;
            logForm.txtLog.Text = logText;
        }
    }
}
